﻿using UnityEngine;
using LuaInterface;
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine.UI;

namespace LuaFramework {
    /// <summary>
    /// 调用lua的静态方法，不能同时使用多个
    /// </summary>
    public class LuaBehaviour : MonoBehaviour {

		public AssetsRef assetsRef = null;
		[Space]

		public bool useRequireLua = true;
        public string luaPath = null;

		private string _name;

		protected void Awake() {
			LuaManager _instance = LuaManager.Instance;
            if (!string.IsNullOrEmpty(luaPath))
            {
				_name = System.IO.Path.GetFileName(luaPath);
				if(useRequireLua){
					LuaManager.Instance.RequireFile(luaPath);
				}else{
					LuaManager.Instance.DoFile(luaPath);
                }
                LuaFunction func = LuaManager.Instance.Lua.GetFunction(_name + ".Awake");
                if (func != null)
                {
                    func.BeginPCall();
                    func.PushObject(gameObject);
					func.PushObject(assetsRef);
                    func.PCall();
                    func.EndPCall();
                    func.Dispose();
                    func = null;
                }
            }
        }


        protected void OnEnable() {
            if (!string.IsNullOrEmpty(_name))
            {
                LuaFunction func = LuaManager.Instance.Lua.GetFunction(_name + ".OnEnable");
                if (func != null)
                {   
                    func.Call();
                    func.Dispose();
                    func = null;
                }
            }
        }

        protected void Start() {
            if (!string.IsNullOrEmpty(_name))
            {
                LuaFunction func = LuaManager.Instance.Lua.GetFunction(_name + ".Start");
                if (func != null)
                {
                    func.Call();
                    func.Dispose();
                    func = null;
                }
            }
        }


        protected void OnDisable() {
            if (!string.IsNullOrEmpty(_name))
            {
                if (!LuaManager.IsInited) return;

                LuaFunction func = LuaManager.Instance.Lua.GetFunction(_name + ".OnDisable");
                if (func != null)
                {
                    func.Call();
                    func.Dispose();
                    func = null;
                }
            }
        }

		public void CallLuaFun(string luaFunc){
            if (!string.IsNullOrEmpty(_name))
            {
                if (!LuaManager.IsInited) return;
                LuaManager mgr = LuaManager.Instance;

                LuaFunction func = mgr.Lua.GetFunction(_name + "." + luaFunc);
                if (func != null)
                {
                    func.Call();
                    func.Dispose();
                    func = null;
                }
            }
		}

		protected void OnDestroy() {
            if (!string.IsNullOrEmpty(_name))
            {
                if (!LuaManager.IsInited) return;
                LuaManager mgr = LuaManager.Instance;

                LuaFunction func = mgr.Lua.GetFunction(_name + ".OnDestroy");
                if (func != null)
                {
                    func.Call();
                    func.Dispose();
                    func = null;
                }
            }
        }
    }
}